import requests
import re


def login():
    # session
    session = requests.session()
    # headers
    session.headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/'
                      '129.0.0.0 Safari/537.36 Edg/129.0.0.0'
    }
    # url1-获取token
    url1 = 'https://github.com/login'
    # 发送请求获取响应
    res_1 = session.get(url1).content.decode()
    # 正则提取
    token = re.findall('name="authenticity_token" value="(.*?)" />', res_1)[0]
    # print(token)
    # url2-登录
    url2 = 'https://github.com/session'
    # 构建表单数据
    data = {
        "commit": "Sign in",
        "authenticity_token": token,
        "add_account": "",
        "login": "lifeprolong",
        "password": "qweljw041013",
        "webauthn - conditional": "undefined",
        "javascript - support": "true",
        "webauthn - support": "supported",
        "webauthn - iuvpaa - support": "supported",
        "return_to": "https: // github.com / login",
        "allow_signup": "",
        "client_id": "",
        "integration": "",
        "required_field_fca3": "",
        "timestamp": "1728909372303",
        "timestamp_secret": "62c1151c3f84ac55e5cb3779cd642ad5565b5c7578bc94380a496ad892748ad1"
    }
    # print(data)
    # 发送请求登录
    session.post(url2, data=data)

    # url3-验证
    url3 = 'https://github.com/lifeprolong'
    response = session.get(url3)
    with open('github.html', 'wb')as f:
        f.write(response.content)


if __name__ == '__main__':
    login()